package service

import (
	"net/url"
	"strings"

	"github.com/mereith/nav/database"
	"github.com/mereith/nav/goscraper"
	"github.com/mereith/nav/logger"
	"github.com/mereith/nav/types"
	"github.com/mereith/nav/utils"
)

func getIcon(url string) string {
	logger.LogInfo("getIcon: %s", url)
	s, err := goscraper.Scrape(url, 5)
	if err != nil {
		logger.LogError("getIcon: %s", err)
		return ""
	}
	var result string = ""
	if strings.Contains(s.Preview.Icon, "http:") || strings.Contains(s.Preview.Icon, "https:") {
		result = s.Preview.Icon
	} else {
		//  如果 link 最后一个是 /
		var first string = s.Preview.Link
		var second string = s.Preview.Icon
		if !strings.Contains(s.Preview.Link[len(s.Preview.Link)-1:len(s.Preview.Link)], "/") {
			first = s.Preview.Link + "/"
		}
		// 如果 icon 第一个是 /
		if strings.Contains(s.Preview.Icon[0:1], "/") {
			second = s.Preview.Icon[1:len(s.Preview.Icon)]
		}
		result = first + second
	}
	logger.LogInfo("getIcon: %s", result)
	return result
}

func LazyFetchLogo(url string, id int64) {
	// 如果 logo 为空，就去获取 logo
	logo := getIcon(url)
	UpdateToolIcon(id, logo)
}

func GetImgFromDB(url1 string) types.Img {
	urlEncoded := url.QueryEscape(url1)
	sql_get_img := `
		SELECT id,url,value FROM nav_img
		WHERE url=?;
		`
	rows, err := database.DB.Query(sql_get_img, urlEncoded)
	utils.CheckErr(err)
	var result types.Img
	var has bool = false
	for rows.Next() {
		err = rows.Scan(&result.Id, &result.Url, &result.Value)
		utils.CheckErr(err)
		has = true
	}
	if !has {
		var nullImg string
		l := strings.Split(url1, ".")
		suffix := l[len(l)-1]
		if strings.Contains(suffix, "svg") {
			nullImg = "PD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI/PjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+PHN2ZyB0PSIxNjQ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AN7IzGcjohwOzXxdMU9bbo4TZyZKXlMUPgvV09UEaTUszpkoM4ptYtG0ffArAHhIPsz8ICLKVdDH0xpNox8nWT0JEeXFxvkAcA4AZP0Gqp2Lj8hmb5KcgO0MF/37niCIgCYDQ2sta5MkKaqzxP3ncS55IcrjzNz8npqaejyr2WYWCU5sNBpChhPlAwAnAcAxWTbG1pbMGHIGzjabrG09Rcn3DEFmzSZnynt7uUhV0PrEpq92IqKQRvYA/p/Wuvnd+hkA5GdPa70EEZ8HAM+Tb2aWfy+Rn+PPETaVFiAreQDHmXmCiLYVUF9hVfQcQVrI1Wq10xCxtcm1uDBE+6uiPUIKZh6vVqvbe7HpPUuQWQv5pfFsIsckOvr40UMDaJdsTcms4fv+jh5q12FN6XmCzJpRXuZ5njx2CVGO6+VOzbFtTwkxtNYyY/wkx3pKY7pvCNJCXI5qNxqNi7XWkv/uvNL0RIkdYea7PM+7rVKp3DQ8PPxkiV3N3LW+I8hsBMMwPAMR3yyfHM95Zd5pRRiU81LMfLt8un1zNA1efU2Q2cDVarWzPc+T+MHyOS0NqF2o6VjYAAABEElEQVSsKwvt27XWt1erVYk40/fFEWSOIRDvPP9hvMn2+z0+SiRZ5ncB4BtEJEmPXJmFgCNIm+EwPj4um3FvYGY5v7QcAE7o8hH0BDPfg4j3VCqV74yMjDzW5e3J1X1HEEt4wzA8Nc5p8UYAkNwWz7c0UbT4LwHgbgD4tpxQDoJAjoG4YoiAI4ghUPOJjY2NHV+pVJYiokStl49EHpfvl6c0basuR1pkT0KO5u9g5h2NRmPHunXrdtoacvLPIeAIktNomJycrDzzzDNLtdYtwhyLiIuZ+ShmXoyIrW/Z5T8KAFrf4tGzALBHvpl5DyLKjvWz8o2Izf8DAMnmusPzvEeXLFmyY+XKlY2cmtLXZv8/whqfIFfJyDkAAAAASUVORK5CYII="
		}

		return types.Img{Id: 0, Url: url1, Value: nullImg}
	}

	defer rows.Close()
	return result
}

func UpdateImg(url1 string) {
	// 除了更新工具本身之外，也要更新 img 表
	// 先看有没有，有的话就不管了，没有的话就创建
	urlEncoded := url.QueryEscape(url1)
	base64ImgValue := utils.GetImgBase64FromUrl(url1)
	if base64ImgValue == "" {
		return
	}
	sql_get_img := `
		SELECT * FROM nav_img
		WHERE url = ?;
		`

	rows, err := database.DB.Query(sql_get_img, urlEncoded)
	utils.CheckErr(err)
	defer rows.Close()
	if !rows.Next() {
		sql_add_img := `
		INSERT INTO nav_img (url, value)
		VALUES (?, ?);
		`
		stmt, err := database.DB.Prepare(sql_add_img)
		utils.CheckErr(err)
		_, err = stmt.Exec(urlEncoded, base64ImgValue)
		utils.CheckErr(err)
	}
}
